package cn.edu.cug.cs.gtl.docsrv.doc.controller;

import cn.edu.cug.cs.gtl.docsrv.dict.exception.BaseException;
import cn.edu.cug.cs.gtl.docsrv.dict.response.PaginatedValues;
import cn.edu.cug.cs.gtl.docsrv.doc.DocManager;
import cn.edu.cug.cs.gtl.docsrv.doc.FTPDocManager;
import cn.edu.cug.cs.gtl.docsrv.doc.HDFSDocManager;
import cn.edu.cug.cs.gtl.docsrv.doc.service.DocService;
import cn.edu.cug.cs.gtl.mybatis.Session;
import cn.edu.cug.cs.gtl.mybatis.mapper.doc.DocAll;
import cn.edu.cug.cs.gtl.mybatis.mapper.doc.DocFile;
import cn.edu.cug.cs.gtl.mybatis.mapper.doc.DocItem;
import cn.edu.cug.cs.gtl.mybatis.metadata.DocUtils;
import cn.edu.cug.cs.gtl.net.ftp.client.ClientFTP;
import cn.edu.cug.cs.gtl.protos.Document;
import cn.edu.cug.cs.gtl.protos.TableInfo;
import cn.edu.cug.cs.gtl.protos.TimeValue;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.sql.Time;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping(value = "/api")
public class DocController {
    @Autowired
    DocService docService;


    @GetMapping(value = "/doc/greetings")

    public List<DocAll> greeting(
            @RequestParam String name){
        try{
            List<DocAll> res = new ArrayList<>();
            DocAll docAll = new DocAll();
            docAll.setDocUrl("bbbbb");
            res.add(docAll);
            DocAll docAll1 = new DocAll();
            docAll1.setDocUrl("cccccccc");
            res.add(docAll1);
            return res;
        }catch (Exception e){
            throw new BaseException(e.getMessage());
        }
    }

    @GetMapping(value = "/doc/getData")
    public List<DocAll> getDocData(){
        try {
            return docService.getDocData();
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    @PostMapping(value = "/doc/testInsertData")
    public void insert() throws Exception{
        String localFile = "C:\\Users\\hong\\testDoc\\test1.doc";
        File file = new File(localFile);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[(int)file.length()];
        fileInputStream.read(bytes);
        fileInputStream.close();
        DocFile docFile = new DocFile();
        docFile.setDocRawBytes(bytes);

        Session session = cn.edu.cug.cs.gtl.docsrv.config.Configuration.getSession();
        ClientFTP clientFTP = ClientFTP.of("124.71.9.22",21,"FTPAdmin","123!@#qwe","/","FTP");
        DocManager docManager = new FTPDocManager(session,clientFTP);
        DocItem docItem = new DocItem();
        docItem.setDocInternalId(new BigDecimal(11));
        docItem.setDocEnTitle("test1");
        docItem.setDocUrl("ftp://202.114.194.1:21/doc/test1.doc");
        docItem.setDocAppendDate(new Date());
        docManager.insert(docItem,docFile);
    }

    @PostMapping(value = "/doc/testInsertData2Hdfs")
    public void insert2hdfs() throws Exception{
        String localFile = "C:\\Users\\hong\\testDoc\\test1.doc";
        File file = new File(localFile);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[(int)file.length()];
        fileInputStream.read(bytes);
        fileInputStream.close();
        DocFile docFile = new DocFile();
        docFile.setDocRawBytes(bytes);
        docFile.setDocInternalId(new BigDecimal(11));
        Session session = cn.edu.cug.cs.gtl.docsrv.config.Configuration.getSession();
        DocManager docManager = new HDFSDocManager(session);
        DocItem docItem = new DocItem();
        docItem.setDocInternalId(new BigDecimal(11));
        docItem.setDocEnTitle("test1");
        docItem.setDocUrl("hdfs://202.114.194.1:9000/doc/test1.doc");
        docItem.setDocAppendDate(new Date());
        docManager.insert(docItem,docFile);
    }


    @PostMapping(value = "/doc/insertData")
    public boolean insertDocData(
            @RequestParam(value = "data") String data){
        try {
            byte [] bytes = Base64.getDecoder().decode(data);
            Document document = Document.parseFrom(bytes);
            DocAll docAll = DocUtils.convert(document);
            return docService.addDocData(docAll);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    @PostMapping(value = "/doc/deleteData")
    public boolean deleteDocData(
            @RequestParam(value = "docInternalId") BigDecimal docInternalId) {
        try {
            return docService.deleteDocData(docInternalId);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }


    @PostMapping(value = "/doc/updateData")
    public boolean updateDocData(
            @RequestParam(value = "data") String data) {
        try {
            byte [] bytes = Base64.getDecoder().decode(data);
            Document document = Document.parseFrom(bytes);
            DocAll docAll = DocUtils.convert(document);
            return docService.updateDocData(docAll);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    //    {
//        "docEnTitle":"Tom",
//        "docCnTitle":"勘查煲噶",
//        "docType":"doc"
//    }

    /**
     * 单条件查询
     * @param column  列名
     * @param value   值
     *
     *       单一条件查询，可以使用的查询条件
     *       private String docEnTitle;
     *       private String docCnTitle;
     *       private String docType;
     * @return
     */
    @GetMapping(value = "/doc/singleQuery")
    public List<DocAll> singleQuery(
            @RequestParam(value = "column") String column,
            @RequestParam(value = "value") String value){
        try {
            return docService.singleQuery(column,value);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }


    @GetMapping(value = "/doc/dateQuery")
    public List<DocAll> dateQuery(
            @RequestParam(value = "begin") String begin,
            @RequestParam(value = "end") String end){
        try {
            byte[] begins = Base64.getDecoder().decode(begin);
            byte[] ends = Base64.getDecoder().decode(end);
            TimeValue timeValue1 = TimeValue.parseFrom(begins);
            TimeValue timeValue2 = TimeValue.parseFrom(ends);
            return docService.dateQuery(timeValue1,timeValue2);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    // application/x-msdownload;
    @PostMapping(value = "/doc/downloadFile")
    public void downloadDocFile(
            @RequestParam(value = "docInternalId") BigDecimal docInternalId,
            HttpServletResponse response) throws Exception {
        response.setContentType("application/x-msdownload; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + "\"");
        try {
            docService.downloadDocFile(docInternalId,response);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }


}
